package cn.bzc.data.process.infra.exception.handler;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@Slf4j
@Component
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public Response handleBusinessException(BizException e) {
        log.error("BizException异常", e);
        return Response.buildFailure(e.getErrCode().getErrCode(),e.getMessage());
    }


    /**
     * Controller上一层相关异常
     *
     * @param e 异常
     * @return 异常结果
     */
//    @ExceptionHandler({
//            NoHandlerFoundException.class,
//            HttpRequestMethodNotSupportedException.class,
//            HttpMediaTypeNotSupportedException.class,
//            HttpMediaTypeNotAcceptableException.class,
//            MissingPathVariableException.class,
//            MissingServletRequestParameterException.class,
//            TypeMismatchException.class,
//            HttpMessageNotReadableException.class,
//            HttpMessageNotWritableException.class,
//            ServletRequestBindingException.class,
//            ConversionNotSupportedException.class,
//            MissingServletRequestPartException.class,
//            AsyncRequestTimeoutException.class
//    })
//    @ResponseBody
//    public Response handleServletException(Exception e) {
//        log.error(e.getMessage(), e);
//        int code = CommonResponseEnum.SERVER_ERROR.getCode();
//        try {
//            ServletResponseEnum[] servletResponseEnums = ServletResponseEnum.values();
//            for (ServletResponseEnum servletResponseEnum : servletResponseEnums) {
//                if(servletResponseEnum.getExceptionName().equals(e.getClass().getSimpleName())){
//                    code = servletResponseEnum.getCode();
//                    break;
//                }
//            }
//        } catch (IllegalArgumentException e1) {
//            log.error("class [{}] not defined in enum {}", e.getClass().getName(), ServletResponseEnum.class.getName());
//        }
//
//
//        return Response.buildFailure(String.valueOf(code),e.getMessage());
//    }





//    /**
//     * 未定义异常
//     *
//     * @param e 异常
//     * @return 异常结果
//     */
//    @ExceptionHandler(value = Exception.class)
//    @ResponseBody
//    public Response handleException(Exception e) {
//        log.error(e.getMessage(), e);
//
//        Response response = Response.buildFailure(String.valueOf(CommonResponseEnum.SERVER_ERROR.getCode()), e.getMessage());
//
//    }
}
